package cn.donting.jme.utile.savable;

import com.jme3.export.InputCapsule;
import com.jme3.export.Savable;
import com.jme3.util.IntMap;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;

public class TxtInputCapsule implements InputCapsule {
    private final int id;
    private final Savable savable;
    private HashMap<String, String> content = new HashMap<>();

    public TxtInputCapsule(String head, String content) throws Exception {
        head = head.substring(2, head.length() - 1);
        String[] split = head.split("@");
        id = Integer.parseInt(split[0]);
        Class<?> aClass = Thread.currentThread().getContextClassLoader().loadClass(split[1]);
        savable = (Savable) aClass.getConstructor().newInstance();
        split = content.split("\n");
        String key = null;
        StringBuilder value = null;
        for (String kv : split) {
            if (kv.startsWith("#")) {
                if (key != null) {
                    this.content.put(key, value.toString());
                    key = null;
                    value = null;
                }
                split = kv.split(":");
                key = split[0].substring(1);
                if(split.length==1){
                    value = new StringBuilder();
                }else {
                    value = new StringBuilder(split[1]);
                }
            } else {
                if(value.length()!=0){
                    value.append("\n");
                }
                value.append(kv.trim());
            }
        }
        if (key != null) {
            String trim = value.toString().trim();
            this.content.put(key, trim);
        }
    }

    int getId() {
        return id;
    }

    Savable getSavable() {
        return savable;
    }

    @Override
    public int getSavableVersion(Class<? extends Savable> clazz) {
        return 0;
    }

    @Override
    public byte readByte(String name, byte defVal) throws IOException {
        return 0;
    }

    @Override
    public byte[] readByteArray(String name, byte[] defVal) throws IOException {
        return new byte[0];
    }

    @Override
    public byte[][] readByteArray2D(String name, byte[][] defVal) throws IOException {
        return new byte[0][];
    }

    @Override
    public int readInt(String name, int defVal) throws IOException {
        return 0;
    }

    @Override
    public int[] readIntArray(String name, int[] defVal) throws IOException {
        return new int[0];
    }

    @Override
    public int[][] readIntArray2D(String name, int[][] defVal) throws IOException {
        return new int[0][];
    }

    @Override
    public float readFloat(String name, float defVal) throws IOException {
        return 0;
    }

    @Override
    public float[] readFloatArray(String name, float[] defVal) throws IOException {
        return new float[0];
    }

    @Override
    public float[][] readFloatArray2D(String name, float[][] defVal) throws IOException {
        return new float[0][];
    }

    @Override
    public double readDouble(String name, double defVal) throws IOException {
        return 0;
    }

    @Override
    public double[] readDoubleArray(String name, double[] defVal) throws IOException {
        return new double[0];
    }

    @Override
    public double[][] readDoubleArray2D(String name, double[][] defVal) throws IOException {
        return new double[0][];
    }

    @Override
    public long readLong(String name, long defVal) throws IOException {
        return 0;
    }

    @Override
    public long[] readLongArray(String name, long[] defVal) throws IOException {
        return new long[0];
    }

    @Override
    public long[][] readLongArray2D(String name, long[][] defVal) throws IOException {
        return new long[0][];
    }

    @Override
    public short readShort(String name, short defVal) throws IOException {
        return 0;
    }

    @Override
    public short[] readShortArray(String name, short[] defVal) throws IOException {
        return new short[0];
    }

    @Override
    public short[][] readShortArray2D(String name, short[][] defVal) throws IOException {
        return new short[0][];
    }

    @Override
    public boolean readBoolean(String name, boolean defVal) throws IOException {
        return false;
    }

    @Override
    public boolean[] readBooleanArray(String name, boolean[] defVal) throws IOException {
        return new boolean[0];
    }

    @Override
    public boolean[][] readBooleanArray2D(String name, boolean[][] defVal) throws IOException {
        return new boolean[0][];
    }

    @Override
    public String readString(String name, String defVal) throws IOException {
        return null;
    }

    @Override
    public String[] readStringArray(String name, String[] defVal) throws IOException {
        return new String[0];
    }

    @Override
    public String[][] readStringArray2D(String name, String[][] defVal) throws IOException {
        return new String[0][];
    }

    @Override
    public BitSet readBitSet(String name, BitSet defVal) throws IOException {
        return null;
    }

    @Override
    public Savable readSavable(String name, Savable defVal) throws IOException {
        return null;
    }

    @Override
    public Savable[] readSavableArray(String name, Savable[] defVal) throws IOException {
        return new Savable[0];
    }

    @Override
    public Savable[][] readSavableArray2D(String name, Savable[][] defVal) throws IOException {
        return new Savable[0][];
    }

    @Override
    public ArrayList readSavableArrayList(String name, ArrayList defVal) throws IOException {
        return null;
    }

    @Override
    public ArrayList[] readSavableArrayListArray(String name, ArrayList[] defVal) throws IOException {
        return new ArrayList[0];
    }

    @Override
    public ArrayList[][] readSavableArrayListArray2D(String name, ArrayList[][] defVal) throws IOException {
        return new ArrayList[0][];
    }

    @Override
    public ArrayList<FloatBuffer> readFloatBufferArrayList(String name, ArrayList<FloatBuffer> defVal) throws IOException {
        return null;
    }

    @Override
    public ArrayList<ByteBuffer> readByteBufferArrayList(String name, ArrayList<ByteBuffer> defVal) throws IOException {
        return null;
    }

    @Override
    public Map<? extends Savable, ? extends Savable> readSavableMap(String name, Map<? extends Savable, ? extends Savable> defVal) throws IOException {
        return null;
    }

    @Override
    public Map<String, ? extends Savable> readStringSavableMap(String name, Map<String, ? extends Savable> defVal) throws IOException {
        return null;
    }

    @Override
    public IntMap<? extends Savable> readIntSavableMap(String name, IntMap<? extends Savable> defVal) throws IOException {
        return null;
    }

    @Override
    public FloatBuffer readFloatBuffer(String name, FloatBuffer defVal) throws IOException {
        return null;
    }

    @Override
    public IntBuffer readIntBuffer(String name, IntBuffer defVal) throws IOException {
        return null;
    }

    @Override
    public ByteBuffer readByteBuffer(String name, ByteBuffer defVal) throws IOException {
        return null;
    }

    @Override
    public ShortBuffer readShortBuffer(String name, ShortBuffer defVal) throws IOException {
        return null;
    }

    @Override
    public <T extends Enum<T>> T readEnum(String name, Class<T> enumType, T defVal) throws IOException {
        return null;
    }


}
